package com.fingard.dsp.bank.directbank.bhlife01;

import java.io.File;
import java.util.Date;
import java.util.HashMap;

import com.fingard.FGBiz;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingleBat;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.io.FileHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;

public class BhLife01SinglePay extends BhLife01Base implements ISinglePay{

	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		throw new Exception("未实现该接口");
	}

	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
		//返回ATS的对象
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		retAts.writeStart();
		if(p_req1944.isByDate){
			Date dateFrom = p_req1944.getBgnDateTime();
			Date dateTo = p_req1944.getEndDateTime();
			HashMap<String,String> seqIDMap = new HashMap<String,String>();
			while(DateHelper.compareDay(dateFrom, dateTo)<=0){
				String dataFilePath = BhLife01Base.getNotifyFilePath(dateFrom);
				File dataFile = new File(dataFilePath);
				if(dataFile.exists()){
					StringBuilder sbText = new StringBuilder();
					sbText.append("<d>\r\n");
					sbText.append(FileHelper.readToString(dataFile, FGBiz.getDefaultCharset()));
					sbText.append("\r\n</d>");
					XmlTextReader xmlRd = new XmlTextReader(sbText);
					while(xmlRd.read("/d/LCCont")){
						XElement eachXE = xmlRd.readedXElement;
						String eachReqSeqID = eachXE.getSingleTextNotNull("EdorAcceptNo");//PolicyNum
						if(!seqIDMap.containsKey(eachReqSeqID)){
							RetAtsSglDetail eachDetail = new RetAtsSglDetail();
							eachDetail.respCode = "0";
							eachDetail.respInfo = eachXE.getSingleTextNotNull("resultMsg");
							eachDetail.payInfoCode = eachXE.getSingleTextNotNull("resultMsg");
							eachDetail.transState = "resultCode+" + eachXE.getSingleTextNotNull("resultCode");
							eachDetail.reqSeqID = eachReqSeqID;
							retAts.writeDetailByDate(eachDetail);
							seqIDMap.put(eachReqSeqID, eachReqSeqID);
						}
					}
				}else{
					WriteBankLogLn("不存在通知数据文件"+dataFilePath);
				}
				dateFrom = DateHelper.addDays(dateFrom, 1);
			}
		}else{
			retAts.writeRespCode("-1");
			retAts.writeRespInfo("请使用按日查询接口");
		}
		retAts.writeEnd();
		return retAts;
	}

	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		throw new Exception("未实现该接口");
	}

	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		throw new Exception("未实现该接口");
	}


}
